#include "CMbmImage.h"
#include "EIKENV.H"
#include <e32std.h>

CMbmImage::CMbmImage()
{
}

CMbmImage::~CMbmImage()
{
	delete iBitmap;
	iBitmap = NULL;
	
	delete iBitmapMask;
	iBitmapMask = NULL;
}

void CMbmImage::ConstructL()
{
	iBitmap = CEikonEnv::Static()->CreateBitmapL(iLocation.iFileName, iLocation.iIndex1);
	if(iLocation.iIndex2!=-1) 
	{
		TRAPD(error,
			iBitmapMask = CEikonEnv::Static()->CreateBitmapL(iLocation.iFileName, iLocation.iIndex2);
		);
	}
	iSize = iBitmap->SizeInPixels();
	iLoaded = ETrue;
}

void CMbmImage::Draw(CBitmapContext& aGc,const TRect& aRc)
{
	if(!iLocation.iValid)
		return;

	TRect rcSrcDraw = TRect(TPoint(0,0),iSize);
	if(IsLoaded())
		{
		if(iSize.iWidth == aRc.Width() && iSize.iHeight == aRc.Height())
			{
			if(iBitmapMask == NULL)
				aGc.BitBlt(aRc.iTl,iBitmap);
			else
				aGc.BitBltMasked(aRc.iTl,iBitmap,rcSrcDraw,iBitmapMask,ETrue);
			}
		else
			{
			if(iBitmapMask == NULL)
				aGc.DrawBitmap(aRc,iBitmap);
			else
				aGc.DrawBitmapMasked(aRc,iBitmap,rcSrcDraw,iBitmapMask,ETrue);
			}
		}
}

void CMbmImage::Draw(CBitmapContext& aGc,const TRect& aRc,TSize aCorner,TInt aOpacity)
{
	if(!iLocation.iValid)
		return;

	CFbsBitmap* mask = CreateMask(aCorner,aOpacity,iSize,ETrue,iBitmapMask);
	if(mask != NULL)
		{
		TRect rcSrcDraw = TRect(TPoint(0,0),iSize);
		if(IsLoaded())
			{
			if(iSize.iWidth == aRc.Width() && iSize.iHeight == aRc.Height())
				{
				aGc.BitBltMasked(aRc.iTl,iBitmap,rcSrcDraw,mask,ETrue);
				}
			else
				{
				aGc.DrawBitmapMasked(aRc,iBitmap,rcSrcDraw,mask,ETrue);
				}
			}
		}
}